<template>
  <div>
    <!-- 用药登记 -->
    <!-- 搜索 -->
    <el-card class="card">
      <el-form ref="ruleFormRef" :inline="true" class="demo-form-inline" :model="params">
        <el-form-item label="老人：" prop="name">
          <el-input v-model="params.name" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="登记日期：" prop="dateTime">
          <el-date-picker
            v-model="dateTime"
            type="daterange"
            unlink-panels
            range-separator="-"
            start-placeholder="开始时间"
            value-format="YYYY-MM-DD"
            end-placeholder="结束时间"
            :shortcuts="shortcuts"
            @change="(e: any) => dateChange(e, params)"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="search">查询</el-button>
          <el-button :icon="Refresh" @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格 -->
    <el-card class="card">
      <ELTable v-bind="tableConfig" ref="medTable">
        <template #addTime="{ scope }">
          {{ dayJs(scope.row.addTime).format("YYYY-MM-DD HH:mm") }}
        </template>
        <template #operate="{ scope }">
          <el-button type="primary" :icon="Document" text @click="toDetail(scope.row.elderlyId)">
            查看详情
          </el-button>
          <el-button type="primary" :icon="Memo" text @click="toPlanset(scope.row.elderlyId)">
            用药计划
          </el-button>
        </template>
        <template #btns="{ deleteAll }">
          <el-button type="success" :icon="Plus" @click="add">用药登记</el-button>
          <el-button type="danger" :icon="Delete" @click="deleteAll">批量删除</el-button>
        </template>
      </ELTable>
      <selectElderly v-if="dialogVisible" url="/medicalcare/add" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { Memo, Delete, Document, Search, Refresh, Plus } from "@element-plus/icons-vue";
import ELTable, { TableData } from "@/components/ELTable.vue";
import selectElderly from "@/components/market/hospitalized/elderly/selectElderly.vue";
import { ListParams } from "@/api/medicalcare/medicinelogs/type";
import { getDrugsList } from "@/api/medicalcare/medicinelogs/medicinelogs";
import dayJs from "dayjs";
import router from "@/router";

const ruleFormRef = ref<FormInstance>();
const medTable = ref();
//搜索参数
const params = reactive<ListParams>({
  page: 1,
  pageSize: 10,
  name: "",
  beginData: "",
  endData: "",
  begId: 0,
});
//创建日期
const dateTime = ref([]);
const shortcuts = [
  {
    text: "最近一周",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      return [start, end];
    },
  },
  {
    text: "最近一个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      return [start, end];
    },
  },
  {
    text: "最近3个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
      return [start, end];
    },
  },
];

//选择时间
const dateChange = (e: any, params: any) => {
  if (e) {
    params.beginDate = e[0];
    params.endDate = e[1];
  } else {
    params.beginDate = "";
    params.endDate = "";
  }
};

//查询
const search = () => {
  medTable.value.getData(params);
};
//重置
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

//表格数据
const tableConfig = reactive<TableData>({
  loadData: getDrugsList,
  columns: [
    {
      prop: "id",
      label: "序号",
    },
    {
      prop: "elderlyName",
      label: "老人姓名",
    },
    {
      prop: "counts",
      label: "用药品种类",
    },
    {
      prop: "addAccountName",
      label: "登记人",
    },
    {
      prop: "addTime",
      label: "最新登记时间",
      slot: true,
    },
    {
      prop: "operate",
      label: "操作",
      slot: true,
      attrs: {
        width: 240,
      },
    },
  ],
});

//查看详情
const toDetail = (id: number) => {
  router.push(`/medicalcare/details/${id}`);
};

//用药登记dialog
const dialogVisible = ref(false);

//添加
const add = () => {
  dialogVisible.value = true;
};

//跳转用药计划
const toPlanset = (id: number) => {
  router.push(`/medicalcare/planset?id=${id}`);
};
</script>

<style scoped>
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>
